package tech.abing.spider.task.crawler;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.pageprocessor.ProductIDProccessor1;
import tech.abing.spider.task.pipeline.ProductIDPipeline;
import tech.abing.spider.util.SqlUtil;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.SysUtil;

public class ProductIDSpider {

	private static Logger logger = Logger.getLogger(ProductIDSpider.class);
	
	private static SqlUtil sqlUtil = null;
	
	public void crawl(){
		this.crawl(1, 3, 1400);//10线程，重试3次，礼貌访问间隔1400ms
	}
	public void crawl(int threads, int retryCount, int politenessDelay){
		CrawTaskBuilder builder = CrawlManager.getInstance()
				.prepareCrawlTask("根据店铺抓取店铺下的所有产品", DefaultPageDownloader.class)
				.useThread(threads)
				.useDynamicEntrance(DynamicEntranceImpl.class)
				.usePageRetryCount(retryCount)//重试3次
				.setPolitenessDelay(politenessDelay)
				.usePipeline(ProductIDPipeline.class)
				.useTaskLifeListener(new TaskLifeListener() {
					
					@Override
					public void onStart(CrawlTask task) {
						if(sqlUtil == null){
							//创建数据库链接
							sqlUtil = SqlUtil.getInstance();
							logger.info("数据库连接创建成功！");
						}
					}
					
					@Override
					public void onFinished(CrawlTask task) {
						if(sqlUtil != null){
							sqlUtil.closeConn();
							logger.info("数据库连接关闭成功！");
						}
						
						task.getPipeline().destory();
					}
				})
				.usePageEncoding(PageEncoding.UTF8);
		
		CrawlTask spider = builder.build();
		
		CrawlManager.getInstance().start(spider);
	}
	
	
	public static final class DynamicEntranceImpl extends DynamicEntrance {

		@Override
		public List<StartContext> loadStartContext() {
			StartContext context = new StartContext();
			
			String shopTable = SysUtil.getValue("shop.table");
			
			while(true){
				//获取店铺
				String sql = "select shop_id, sale_count,type from "+shopTable+" order by sort,sale_count desc limit 1";
				String delSql = "delete from "+shopTable+" where shop_id = ?";
				
				Map<String, Object> map = sqlUtil.searchForConn(sql);
				if(map != null && !map.isEmpty()){
					String shopId = map.get("shop_id").toString();
					
					int cnt = sqlUtil.deleteForConn(delSql,shopId);
					if(cnt > 0){//删除成功
						
						//按销量排序
						String shopUrl = "https://shop"+shopId+".taobao.com/search.htm?scene=taobao_shop&search=y&orderType=hotsell_desc&tsearch=y";
						String saleCount = StringUtils.toString(map.get("sale_count"));
						String type = StringUtils.toString(map.get("type"));
						logger.info("=========>店铺："+shopUrl);
						PageRequest req = context.createPageRequest(shopUrl, ProductIDProccessor1.class, 0);
						req.putParams("shopId", shopId);
						req.putParams("saleCount", saleCount);
						req.putParams("type", type);
						context.injectSeed(req);
						break;//取到任务，去抓取
					}else{
						continue;//继续取任务
					}
				}else{
					break;//没任务了，直接跳出
				}
			}
			return Arrays.asList(context);
		}
		public boolean continueLoad() {
			return true;//循环采集
		}
	}
	
}
